// pages/order/tabs/index.js
import Common from '../../../utils/common.js'
let http = new Common()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: ['全部订单', '待付款', '待欢唱'],
    tabIndex: '0',
    info: [],
    page: '1'
  },
  tabClick: function (e) {
    this.setData({
      tabIndex: e.currentTarget.dataset.index,
      page: '1',
      info: [],
      totalpage: ''
    })
    if (this.data.tabIndex == '0') {

      this.getData()


    } else if (this.data.tabIndex == '1') {

      this.getData1()

    } else if (this.data.tabIndex == '2') {

      this.getData2()

    }
    // console.log(e.currentTarget.dataset)
  },
  getData: function () {
    let that = this
    let data = {
      viptel: this.data.tel,
      page: this.data.page
    }
    http.http({
      url: 'mp2/store/getallorder',
      data: data,
      success(res) {
        
        if(that.data.info.length>0){
          let info = that.data.info
          res.data.data.allorder.forEach(val=>{
            info.push(val)
          })
          that.setData({
            info: info,
            totalpage: res.data.data.totalpage
          })
        }else{
          that.setData({
            info: res.data.data.allorder,
            totalpage: res.data.data.totalpage
          })
        }
        

        wx.hideLoading()
        wx.stopPullDownRefresh()
      }

    })

  },
  getData1: function () {
    let that = this
    let data = {
      viptel: this.data.tel,
      page: this.data.page
    }
    http.http({
      url: 'mp2/store/tobepaied',
      data: data,
      success(res) {

        if (that.data.info.length > 0) {
          let info = that.data.info
          res.data.data.allorder.forEach(val => {
            info.push(val)
          })
          that.setData({
            info: info,
            totalpage: res.data.data.totalpage
          })
        } else {
          that.setData({
            info: res.data.data.allorder,
            totalpage: res.data.data.totalpage
          })
        }

        // that.setData({
        //   info: res.data.data.allorder,
        //   index1: res.data.data.allorder.length,
        //   totalpage: res.data.data.allorder.totalpage
        // })
        wx.hideLoading()
        wx.stopPullDownRefresh()
      }

    })

  },
  getData2: function () {
    let that = this
    let data = {
      viptel: this.data.tel,
      page: this.data.page
    }
    http.http({
      url: 'mp2/store/waitsinglist',
      data: data,
      success(res) {

        if (that.data.info.length > 0) {
          let info = that.data.info
          res.data.data.allorder.forEach(val => {
            info.push(val)
          })
          that.setData({
            info: info,
            totalpage: res.data.data.allorder.totalpage
          })
        } else {
          that.setData({
            info: res.data.data.allorder,
            totalpage: res.data.data.totalpage
          })
        }

        // that.setData({
        //   info: res.data.data.allorder,
        //   index2: res.data.data.allorder.length,
        //   totalpage: res.data.data.allorder.totalpage
        // })
        wx.hideLoading()
        wx.stopPullDownRefresh()
      }
    })

  },
  click: function (e) {
    let info = this.data.info
    let index = e.currentTarget.dataset.index
    let that = this
    // console.log(info[index])
    if (info[index].status == '待支付') {
      http.http({
        url: 'mp2/store//gotopay',
        data: {
          viptel: that.data.tel,
          orderNo: info[index].orderNo
        },
        success(res) {
          if (res.data.data.info) {
            http.goUrl({
              url: `../../payment/cancelPay/index?id=${info[index].orderNo}`
            })
          }
        }
      })
    } else if (info[index].status == '待欢唱' || info[index].status == '已完成') {
      http.goUrl({
        url: `../tabDetails/index?id=${'1'}&&orderNum=${info[index].orderNo}`
      })
    } else {
      wx.switchTab({
        url: '../../index/homePage/index'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options')
    let tel = wx.getStorageSync('tel')
    this.setData({
      tel: tel
    })

    if (options.id) {
      this.setData({
        tabIndex: '1'
      })
      this.getData1()
    } else {
      this.getData()
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: '1',
      info: [],
    })

    if (this.data.tabIndex == '0') {

      this.getData()


    } else if (this.data.tabIndex == '1') {

      this.getData1()

    } else if (this.data.tabIndex == '2') {

      this.getData2()

    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if ((this.data.page) < this.data.totalpage) {
      wx.showLoading({
        title: '加载中...',
        mask: true,
      })

      let page = this.data.page
      page++
      this.setData({
        page: page
      })
      if (this.data.tabIndex == '0') {

        this.getData()


      } else if (this.data.tabIndex == '1') {

        this.getData1()

      } else if (this.data.tabIndex == '2') {

        this.getData2()

      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})